require(['grid', 'topBar', 'date', 'select2'], function () {
	$grid = $("#grid");
	//初始化顶部搜索栏
	function topBarInit() {
        $("#top").topBar({
            left: [
                {
                    type: 'filter-more',
                    dateType:'custom',
                    search: function () {
                        search();
                    },
                    item: [
                        {
                            text: '时间快捷',
                            name1: 'beginTime',
                            name2: 'endTime',
                            type: 'dateLine'
                        },
                        {
        				    text: '所属企业',
        				    name: 'corpId',
        				    option: {
        				        url: '/sysCorpInfo/getCorpTree.htm',
        				        name: 'corpName',
        				        idKey: 'corpId',
        				        pIdKey: 'parentCorpId'
        				    },
        				    width: 150,
        				    type: 'text-tree'
        				},
                        {
                            text: '功能名称',
                            name: 'qMnpltFuncName',
                            width: '80',
                            type: 'text'
                        },
                        {
                            text: '用户帐号',
                            name: 'qAcctName',
                            width: '80',
                            type: 'text'
                        },
                        {
                            text: '用户名称',
                            name: 'qUserName',
                            width: '80',
                            type: 'text'
                        },
                        {
                            text: '操作类型',
                            name: 'qMnpltType',
                            width: '160',
                            type: 'select',
                            dict:'OLOG0000'
                        }
                    ]
                }
            ],
            right: [
                {
				    text: '查询',
				    type: 'btn',
//				    authority: '',
				    click: function () {
				    	search();
				    }
				},
				{
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                                exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                                exportFile('Pdf');
                            }
                        }
                    ]
                }
            ]
        });
    }
	
	$grid.grid({
		url : "/reportOperatDate/searchOperateLogPage.htm",
		width:$(window).width() - 30,
		height:$(window).height()-131,
		//sortorder: "desc",
		//sortname: 'smnplt_Time',
		colModel:[
			{name: "id", index: "id", key: true, hidden: true, label: 'id'},
			{name: "mnpltType", index: "mnpltType", dict: "OLOG0000",width:100, label: '操作类型'},
			{name: "mnpltFuncName", index: "mnpltFuncName",width:380, label: '功能名称'},
			{name: "acctName", index: "acctName",width:100, label: '用户帐号'},
			{name: "corpName", index: "corpName",width:100, label: '帐号所属公司'},
			{name: "userName", index: "userName",width:100,label: '用户名称'},
			{name: "smnpltTime", index: "MNPLT_TIME",width:150,sortable:true, label: '操作时间'},
			{name: "smnpltDura", index: "MNPLT_DURA",width:110,sortable:true, label: '响应时长'},
			{name: "ipAdr", index: "ipAdr",width:110, label: 'IP地址'},
			{name: "ipDetail", index: "ipDetail",width:150, label: 'IP来源'},
			{name: "mnpltUrl", index: "mnpltUrl", label: '操作url'},
			{name: "macAdr", index: "macAdr",width:150, label: 'MAC地址'}],
		showPaper : true,
		gridComplete : function(){
			var ids = $grid.jqGrid('getDataIDs');
			for(var i = 0; i < ids.length; i++) {
				var id = ids[i];
				var rowObject = $grid.jqGrid('getRowData', id);
				var ip = rowObject.ipAdr;
				setIpDetail(id,ip);
			}
		}});
	
	function search(){
		$('#grid').grid('setGridParam', {
            page:1,
            datatype: "json",
            postData: $("#top").parent().getValues()
        }).trigger("reloadGrid");
	}
	
	function exportFile(suffix){
        if($('#grid').getGridParam("reccount")>0){
            var url = "/reportOperatDate/exportOperateLog"+suffix+".htm";
            $.exportFile(url, $("#top").parent().getValues());
        }
        else{
            $.messager.alert("提示","请先查询数据。","info");
        }
    }
	
	
	function setIpDetail(id,ip){
		$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' + ip, function () {
			var content = "";
			if(remote_ip_info){
				if (remote_ip_info.ret == '1') {
					content = remote_ip_info.country + " " + remote_ip_info.province + " " + remote_ip_info.city + " " + remote_ip_info.isp + " " + remote_ip_info.type;
				} else {
					content = "未知";
				}
			}
			$grid.jqGrid("setCell", id,"ipDetail",content);
		});
	}
	topBarInit();
	
});
